package com.heima.hellospring.api;

import com.heima.hellospring.domain.Teacher;
import com.heima.hellospring.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/teacher")
public class TeacherApiController {
    @Autowired
    private TeacherService teacherService;

    @GetMapping("/list")
    public List<Teacher> findAll(){
        List<Teacher> teachers = teacherService.findAll();
        return teachers;
    }
    //根据姓名查找教师数据
    @GetMapping("/{name}")
    public Teacher queryTeacher(@PathVariable String name){
        List<Teacher> teachers= teacherService.findByTname(name);
        return teachers.get(0);
    }

    //根据id查找教师数据
    @GetMapping("/list/{id}")
    public Teacher queryTeacher(@PathVariable("id") Integer id){
        Teacher teacher= teacherService.findById(id);
        return teacher;
    }

    //新增教师信息
    @PostMapping("/add")
    public Teacher addTeacher(Teacher teacher){
        return teacherService.insert(teacher);
    }

    //修改教师信息
    @PutMapping("/update")
    public Teacher updateTeacher(Teacher teacher){
        return teacherService.update(teacher);
    }

    //删除教师数据
    @DeleteMapping("/delete/{id}")
    public void deleteTeacher(@PathVariable Integer id){
        teacherService.delete(id);
    }
}
